import { API_CMS_URL, API_ADMIN_CMS_URL } from '@/config/api';
import { AnyObject, HttpResponse, PageResponse } from '@/types/global';
import axios from 'axios';
import {
  CmsArticle,
  CmsAuthor,
  CmsColumn,
  CmsComment,
  CmsExam,
  CmsExamQuestion,
} from '@/types/cms';

export function createAuthor(data: AnyObject) {
  return axios.post(`${API_ADMIN_CMS_URL}/author`, data);
}

export function updateAuthor(data: AnyObject) {
  return axios.put(`${API_ADMIN_CMS_URL}/author`, data);
}

export function deleteAuthor(id: string) {
  return axios.delete(`${API_ADMIN_CMS_URL}/author/${id}`);
}

export function getAuthorList(data: AnyObject) {
  return axios.post<HttpResponse<PageResponse<CmsAuthor>>>(
    `${API_CMS_URL}/author/page`,
    data
  );
}

export function createColumn(data: AnyObject) {
  return axios.post(`${API_ADMIN_CMS_URL}/column`, data);
}

export function updateColumn(data: AnyObject) {
  return axios.put(`${API_ADMIN_CMS_URL}/column`, data);
}

export function deleteColumn(id: string) {
  return axios.delete(`${API_ADMIN_CMS_URL}/column/${id}`);
}

export function getColumnList(data: AnyObject) {
  return axios.post<HttpResponse<CmsColumn[]>>(
    `${API_CMS_URL}/column/list`,
    data
  );
}

export function createArticle(data: AnyObject) {
  return axios.post(`${API_ADMIN_CMS_URL}/article`, data);
}

export function updateArticle(data: AnyObject) {
  return axios.put(`${API_ADMIN_CMS_URL}/article`, data);
}

export function deleteArticle(id: string) {
  return axios.delete(`${API_ADMIN_CMS_URL}/article/${id}`);
}

export function getArticleList(data: AnyObject) {
  return axios.post<HttpResponse<PageResponse<CmsArticle>>>(
    `${API_CMS_URL}/article/page`,
    data
  );
}

export function getArticleDetail(id: string) {
  return axios.get<HttpResponse<CmsArticle>>(
    `${API_CMS_URL}/article/detail?id=${id}`
  );
}

export function getCommentList(data: AnyObject) {
  return axios.post<HttpResponse<PageResponse<CmsComment>>>(
    `${API_ADMIN_CMS_URL}/comment/page`,
    data
  );
}

export function approveComment(id: number, state: number) {
  return axios.post(
    `${API_ADMIN_CMS_URL}/comment/approve/${id}?state=${state}`
  );
}

export function createExam(data: AnyObject) {
  return axios.post<HttpResponse<CmsExam>>(`${API_ADMIN_CMS_URL}/exam`, data);
}

export function updateExam(data: AnyObject) {
  return axios.put<HttpResponse<CmsExam>>(`${API_ADMIN_CMS_URL}/exam`, data);
}

export function getExamDetail(articleId: string) {
  return axios.get<HttpResponse<CmsExam>>(
    `${API_ADMIN_CMS_URL}/exam/detail?articleId=${articleId}`
  );
}

export function deleteExam(id: string) {
  return axios.delete<HttpResponse<CmsExam>>(`${API_ADMIN_CMS_URL}/exam/${id}`);
}

export function updateQuestion(data: AnyObject[]) {
  return axios.post(`${API_ADMIN_CMS_URL}/exam/question`, data);
}

export function getQuestion(examId: string) {
  return axios.post<HttpResponse<CmsExamQuestion[]>>(
    `${API_ADMIN_CMS_URL}/exam/question/list?examId=${examId}`
  );
}
